******SET YOUR DIRECTORY to ...\replication



///////////////////////////////////////////////////////////////////////////////////////////
///Table A.55
///////////////////////////////////////////////////////////////////////////////////////////
use "finaldata\final_cc_data.dta", clear

drop if fipsplace_00==.
xtset fipsplace_00 year

keep if year==1980

g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
gen bad_soil=1-good_soil

keep FMSA bad_soil

rename FMSA metaread

tempfile cc_soil
save `cc_soil'


use cluster perwt strata metaread metro migtype5 race marrno educ empstat pwtype valueh rent nchild birthyr sex year ///
		    acreprop condo builtyr unitsstr using "finaldata\census7080_micro.dta", clear

***Define weights
svyset cluster [pweight=perwt], strata(strata)

***Use only 1980 (in 1970 know only if moves between MSA but not if between CC and sub)
keep if year==1980

///Merge
merge m:1 metaread using `cc_soil'
codebook metaread if _merge==3
*232 cities

drop if _merge!=3

***Define suburbanized
gen livemsa=(metro==2|metro==3|metro==4)
g sub=(metro==3)
replace sub=. if metro==0 | metro==1 | metro==4
label var sub "Suburbanized now"
g sub5=2 if (sub==1 & migtype5==3)
replace sub5=1 if (sub==1 & migtype5==4)
replace sub5=0 if sub==0
label var sub5 "Suburbanization today and 5 years ago"
label define sub5l 2 "Sub today, CC past" 1 "Sub today, sub past" 0 "CC today"
label values sub5 sub5l
gen justsub5=(sub==2)

***Generate variables
gen white=(race==1)
gen black=(race==2)
gen married=(marrno==1|marrno==2)
gen hshigher=(educ>=6 & educ!=.)
gen employed=(empstat==1)
gen unemployed=(empstat==2)
gen work_cc=(pwtype==1|pwtype==2|pwtype==3)
gen work_ncc=(pwtype==4)
replace work_ncc=. if pwtype==0 | pwtype>4
replace work_cc=. if pwtype==0 | pwtype>4
gen whitehshigher=(white==1&hshigher==1)
replace whitehshigher=. if white==0
gen logvalueh=log(valueh)
gen logrent=log(rent)
gen ncc=1 if metro==3
replace ncc=0 if metro==2
gen bad_soil_black=bad_soil*black
gen bad_soil_ncc=bad_soil*ncc
gen black_ncc=black*ncc
gen bad_soil_black_ncc=bad_soil*black*ncc

***Label
label var bad_soil "High LB"
label var bad_soil_black "High LB x Black"
label var bad_soil_ncc "High LB x Live in NCC"
label var bad_soil_black_ncc "High LB x Black x Live in NCC"
label var black_ncc "Black x Live in NCC"
label var black "Black"
label var ncc "Live in NCC"

keep if sub5!=1 & livemsa==1

svy: reg sub5 bad_soil nchild birthyr i.educ i.empstat i.race i.sex if sub5!=1 & livemsa==1
outreg2 using table_whiteflight, keep(bad_soil) tex(frag) nocon noni nor2 nonotes label ctitle("From CC to NCC") addtext(Individual controls, YES, Housing controls, NO, MSA FE, NO, Year FE, NO, C. div x Year, NO, Sample, In CC 5 yr ago, Estimation, OLS) replace
svy: reg sub5 bad_soil black bad_soil_black nchild birthyr i.educ i.empstat i.race i.sex if sub5!=1 & livemsa==1
outreg2 using table_whiteflight, keep(bad_soil black bad_soil_black) tex(frag) nocon noni nor2 nonotes label ctitle("From CC to NCC") addtext(Individual controls, YES, Housing controls, NO, MSA FE, NO, Year FE, NO, C. div x Year, NO, Sample, In CC 5 yr ago, Estimation, OLS)




///////////////////////////////////////////////////////////////////////////////////////////
///Table A.56 -A.57 - A.58
///////////////////////////////////////////////////////////////////////////////////////////
use "finaldata\final_cc_data.dta", clear

drop if fipsplace_00==.
xtset fipsplace_00 year

drop if year>1991
drop if year==1950

***check _merge==1
merge n:1 fipsplace_00 using  "finaldata\top10_CC.dta"
drop if _merge==1

*Neighbors of first neighbor
forval i = 1/3 {
levelsof fipsplace_00, local(levels) 
foreach l of local levels {
	qui: su targetid`i' if fipsplace_00 == `l'
	g targetid`i'_`l' = r(mean)
}
}

forval j=1/3{
gen targetid`j'_1=.
gen targetid`j'_2=.
gen targetid`j'_3=.

forval i = 1/3 {
	foreach l of local levels {
		qui: su targetid`j' if fipsplace_00 == `l'
		local neighbor = r(mean)
		replace targetid`j'_`i' = targetid`i'_`neighbor' if fipsplace_00 == `l'
	}
}

}
*pH of neighbors
levelsof fipsplace_00, local(levels) 
foreach l of local levels {
	qui: su ph1_plc_wtm_wtm_0_r if fipsplace_00 == `l'
	g ph_`l' = r(mean)
}

forval i = 1/10 {
	g ph_nei_`i' = .
	foreach l of local levels {
		qui: su targetid`i' if fipsplace_00 == `l'
		local neighbor = r(mean)
		replace ph_nei_`i' = ph_`neighbor' if fipsplace_00 == `l'
	}
}

forval j = 1/3 {
forval i = 1/3 {
	g ph_nei_`j'_nei_`i' = .
	foreach l of local levels {
		qui: su targetid`j'_`i' if fipsplace_00 == `l'
		local neighbor = r(mean)
		replace ph_nei_`j'_nei_`i' = ph_`neighbor' if fipsplace_00 == `l'
	}
}
}

drop ph_101852-ph_5613900


*Create instrument and outcome
g good_soil = (ph1_plc_wtm_wtm_0_r>=6.8 & ph1_plc_wtm_wtm_0_r<=7.7)
gen inter_goodtetra=good_soil*tetra_corr
gen inter_badtetra=(1-good_soil)*tetra_corr
g perc_cc =  tot_cc_oripop_corr / (tot_ncc_oripop_corr + tot_cc_oripop_corr)
g pop_msa = tot_ncc_oripop_corr + tot_cc_oripop_corr

*Create variables
gen vc_pc=totnpcc_cc_offenses_vc*1000

gen vc_ncc_pc=totnpcc_ncc_offenses_vc*1000

*Violent crime neighbors
forval i = 1/10 {
	g vc_pc_nei_`i' = .
}

forval yy=1960/1991 {
	qui: levelsof fipsplace_00, local(levels) 
		foreach l of local levels {
		qui: su vc_pc if fipsplace_00 == `l' & year==`yy'
		g vc_pc_`l' = r(mean)
	}
	
	forval i = 1/10 {
		foreach l of local levels {
			qui: su targetid`i' if fipsplace_00 == `l'
			local neighbor = r(mean)
			replace vc_pc_nei_`i' = vc_pc_`neighbor' if fipsplace_00 == `l' & year==`yy'
		}
	}
	drop vc_pc_101852 - vc_pc_5613900
}

g lnpop_cc = ln(tot_cc_oripop_corr)
g lnpop_ncc = ln(tot_ncc_oripop_corr)
gen lnpop_msa=ln(pop_msa)


*Cluster variables
egen cdivcodeyear=group(cdivcode year)

*Label variables
label var vc_pc "Violent crime CC"
label var inter_badtetra "High LB x Lead"
label var cpctfst "Sh. foreign pop. CC"
label var perc_cc "Sh. Pop CC"
*label var l10perc_cc "Sh. Pop CC (10 years lag)"

forval i = 1/10 {
	g good_soil_nei_`i' = (ph_nei_`i'>=6.8 & ph_nei_`i'<=7.7)
	g inter_goodtetra_nei_`i'=good_soil_nei_`i'*tetra_corr
	g inter_badtetra_nei_`i'=(1-good_soil_nei_`i')*tetra_corr
	replace distance`i' = distance`i' / 1000
}



gen bad_soil=1-good_soil
forvalues i=1/5{
gen bad_soil_nei_`i'=1-good_soil_nei_`i'
}
egen bad_soil_nei_1_3=rowmean(bad_soil_nei_1 bad_soil_nei_2 bad_soil_nei_3)
gen inter_badtetra_nei_1_3=bad_soil_nei_1_3*tetra_corr
egen bad_soil_nei_1_5=rowmean(bad_soil_nei_1 bad_soil_nei_2 bad_soil_nei_3 bad_soil_nei_4 bad_soil_nei_5)
gen inter_badtetra_nei_1_5=bad_soil_nei_1_5*tetra_corr

egen vc_pc_nei_1_3=rowmean(vc_pc_nei_1 vc_pc_nei_2 vc_pc_nei_3)
egen vc_pc_nei_1_5=rowmean(vc_pc_nei_1 vc_pc_nei_2 vc_pc_nei_3 vc_pc_nei_4 vc_pc_nei_5)

label var vc_pc_nei_1 "Violent crime CC 1st neighbor"
label var inter_badtetra_nei_1 "High LB 1st neighbor x Lead"
label var vc_pc_nei_1_3 "Av. violent crime CC 3 neighbors"
label var inter_badtetra_nei_1_3 "Av. high LB 3 neighbors x Lead"
label var vc_pc_nei_1_5 "Av. violent crime CC 5 neighbors"
label var inter_badtetra_nei_1_5 "Av. high LB 5 neighbors x Lead"

label var lnpop_cc "ln(Pop CC)"
label var lnpop_ncc "ln(Pop NCC)"
label var lnpop_msa "ln(Pop MSA)"

***Set sample
reghdfe perc_cc vc_pc vc_pc_nei_1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
g SAMPLE = e(sample)==1

***Controlling for VC NEI 1
*First stage
reghdfe vc_pc inter_badtetra inter_badtetra_nei_1 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei1_1
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
estadd local ff ".", replace
*OLS
reghdfe perc_cc vc_pc vc_pc_nei_1 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei1_2
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
estadd local ff ".", replace
*IV
ivreghdfe perc_cc vc_pc_nei_1 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
local ff= string(round(e(cdf),.01) , "%9.2f")
est sto nei1_3
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV CC
ivreghdfe lnpop_cc vc_pc_nei_1 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei1_4
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV NCC
ivreghdfe lnpop_ncc vc_pc_nei_1 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei1_5
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV MSA
ivreghdfe lnpop_msa vc_pc_nei_1 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei1_6
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace

***Controlling for VC NEI 1-3
*First stage
reghdfe vc_pc inter_badtetra inter_badtetra_nei_1_3 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei13_1
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
estadd local ff ".", replace
*OLS
reghdfe perc_cc vc_pc vc_pc_nei_1_3 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei13_2
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
estadd local ff ".", replace
*IV
ivreghdfe perc_cc vc_pc_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
local ff= string(round(e(cdf),.01) , "%9.2f")
est sto nei13_3
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV CC
ivreghdfe lnpop_cc vc_pc_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei13_4
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV NCC
ivreghdfe lnpop_ncc vc_pc_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei13_5
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV MSA
ivreghdfe lnpop_msa vc_pc_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei13_6
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace


***Controlling for VC NEI 1-5
*First stage
reghdfe vc_pc inter_badtetra inter_badtetra_nei_1_5 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei15_1
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
estadd local ff ".", replace
*OLS
reghdfe perc_cc vc_pc vc_pc_nei_1_5 if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei15_2
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
estadd local ff ".", replace
*IV
ivreghdfe perc_cc vc_pc_nei_1_5 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
local ff= string(round(e(cdf),.01) , "%9.2f")
est sto nei15_3
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV CC
ivreghdfe lnpop_cc vc_pc_nei_1_5 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei15_4
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV NCC
ivreghdfe lnpop_ncc vc_pc_nei_1_5 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei15_5
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV MSA
ivreghdfe lnpop_msa vc_pc_nei_1_5 (vc_pc = inter_badtetra) if SAMPLE==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto nei15_6
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace

***Table
esttab nei1_1 nei1_2 nei1_3 nei1_4 nei1_5 nei1_6 using  table_vcnei_control.tex, ///
	   prehead("\begin{tabular}{l*{6}{c}} \hline\hline") ///
	   posthead("\hline \\ \multicolumn{7}{c}{Panel A: Controlling for violent crime of closest neighbor} \\\\[-1ex]") ///
	   fragment label nocon noobs se ///
	   s(N msafe yearfe cdivyfe estim ff, label("Observations" "MSA FE" "Year FE" "C. div x Year" "Estimation" "F")) ///
	   star(* 0.10 ** 0.05 *** 0.01) replace
	   
esttab nei13_1 nei13_2 nei13_3 nei13_4 nei13_5 nei13_6 using  table_vcnei_control.tex, ///
	   posthead("\hline \\ \multicolumn{7}{c}{Panel B: Controlling for violent crime of three closest neighbors} \\\\[-1ex]") ///
	   fragment append nomtitles nonumbers ///
	   label nocon se ///
	   nolines s(N msafe yearfe cdivyfe estim ff, label("Observations" "MSA FE" "Year FE" "C. div x Year" "Estimation" "F")) ///
	   star(* 0.10 ** 0.05 *** 0.01) 
	   
esttab nei15_1 nei15_2 nei15_3 nei15_4 nei15_5 nei15_6 using  table_vcnei_control.tex, ///
	   posthead("\hline \\ \multicolumn{7}{c}{Panel C: Controlling for violent crime of five closest neighbors} \\\\[-1ex]") ///
	   fragment append nomtitles nonumbers ///
	   label nocon se ///
	   nolines s(N msafe yearfe cdivyfe estim ff, label("Observations" "MSA FE" "Year FE" "C. div x Year" "Estimation" "F")) ///
	   star(* 0.10 ** 0.05 *** 0.01) ///
	   prefoot("\hline") ///
	   postfoot("\hline\hline \end{tabular}")

	   
	   
	   
	   forval i=1/5{
gen vc_pc_nei_`i'_60_2=vc_pc_nei_`i' if year==1960
egen vc_pc_nei_`i'_60=max(vc_pc_nei_`i'_60_2),by(fipsplace_00)
drop vc_pc_nei_`i'_60_2
}

 
  

 
label var vc_pc_nei_1 "Violent crime CC 1st neighbor"
label var vc_pc_nei_2 "Violent crime CC 2nd neighbor"
label var vc_pc_nei_3 "Violent crime CC 3rd neighbor"
label var vc_pc_nei_4 "Violent crime CC 4th neighbor"
label var vc_pc_nei_5 "Violent crime CC 5th neighbor"

label var vc_pc_nei_1_60 "Violent crime CC 1960 1st neighbor"
label var vc_pc_nei_2_60 "Violent crime CC 1960 2nd neighbor"
label var vc_pc_nei_3_60 "Violent crime CC 1960 3rd neighbor"
label var vc_pc_nei_4_60 "Violent crime CC 1960 4th neighbor"
label var vc_pc_nei_5_60 "Violent crime CC 1960 5th neighbor"

label var inter_badtetra_nei_1 "High LB 1st neighbor x Lead"
label var inter_badtetra_nei_2 "High LB 2nd neighbor x Lead"
label var inter_badtetra_nei_3 "High LB 3rd neighbor x Lead"
label var inter_badtetra_nei_4 "High LB 4th neighbor x Lead"
label var inter_badtetra_nei_5 "High LB 5th neighbor x Lead"

label var vc_pc_nei_1_3 "Av. violent crime CC 3 neighbors"
label var inter_badtetra_nei_1_3 "Av. high LB 3 neighbors x Lead"
label var vc_pc_nei_1_5 "Av. violent crime CC 5 neighbors"
label var inter_badtetra_nei_1_5 "Av. high LB 5 neighbors x Lead"
label var bad_soil "High LB"
label var bad_soil_nei_1 "High LB 1st neighbor"
label var bad_soil_nei_2 "High LB 2nd neighbor"
label var bad_soil_nei_3 "High LB 3rd neighbor"
label var bad_soil_nei_4 "High LB 4th neighbor"
label var bad_soil_nei_5 "High LB 5th neighbor"

label var lnpop_cc "ln(Pop CC)"
label var lnpop_ncc "ln(Pop NCC)"
label var lnpop_msa "ln(Pop MSA)"

*Bad soil
reghdfe bad_soil bad_soil_nei_1 bad_soil_nei_2 bad_soil_nei_3 bad_soil_nei_4 bad_soil_nei_5 if year==1991, abs(cdivcode)
outreg2 vc_pc using  table_vcnei_identif, tex(frag) nocon noni nor2 nonotes label ctitle("High LB") addtext(MSA FE, NO, Year FE, NO, C. div FE, YES, C. div x Year, NO, Sample, Cross-section, Estimation, OLS) replace
*Instrument
reghdfe inter_badtetra inter_badtetra_nei_1 inter_badtetra_nei_2 inter_badtetra_nei_3 inter_badtetra_nei_4 inter_badtetra_nei_5, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
outreg2 vc_pc using table_vcnei_identif, tex(frag) nocon noni nor2 nonotes label ctitle("High LB x Lead") addtext(MSA FE, YES, Year FE, YES, C. div FE, NO, C. div x Year, YES, Sample, Panel, Estimation, OLS) 
*Vc neighbor
reghdfe bad_soil vc_pc_nei_1_60 vc_pc_nei_2_60 vc_pc_nei_3_60 vc_pc_nei_4_60 vc_pc_nei_5_60 if year==1991, abs(cdivcode)
outreg2 vc_pc using  table_vcnei_identif, tex(frag) nocon noni nor2 nonotes label ctitle("High LB") addtext(MSA FE, NO, Year FE, NO, C. div FE, YES, C. div x Year, NO, Sample, Cross-section, Estimation, OLS)




 
 
forval j = 1/3 {
forval i = 1/3 {
	g good_soil_nei_`j'_nei_`i' = (ph_nei_`j'_nei_`i'>=6.8 & ph_nei_`j'_nei_`i'<=7.7)
	g bad_soil_nei_`j'_nei_`i'=1-good_soil_nei_`j'_nei_`i'
	
	g good_soil_nei_`j'_nei_`i'_corr = (ph_nei_`j'_nei_`i'>=6.8 & ph_nei_`j'_nei_`i'<=7.7) if targetid`j'_`i'!=fipsplace_00
	g bad_soil_nei_`j'_nei_`i'_corr=1-good_soil_nei_`j'_nei_`i'
	
}
}

egen bad_soil_nei_1_0_3_corr=rowmean(bad_soil_nei_1_nei_1_corr bad_soil_nei_1_nei_2_corr bad_soil_nei_1_nei_3_corr )
gen inter_badtetra_nei_1_0_3_corr=bad_soil_nei_1_0_3_corr*tetra_corr

egen bad_soil_nei_2_3=rowmean(bad_soil_nei_2 bad_soil_nei_3)
gen inter_badtetra_nei_2_3=bad_soil_nei_2_3*tetra_corr

gen inter_bad_vc_nei_1_60=inter_badtetra*vc_pc_nei_1_60

label var lnpop_cc "ln(Pop CC)"
label var lnpop_ncc "ln(Pop NCC)"
label var lnpop_msa "ln(Pop MSA)"
label var vc_pc_nei_1 "Violent crime CC 1st neighbor"
label var inter_badtetra_nei_1_3 "Av. high LB 3 neighbors x Lead"
label var inter_badtetra_nei_2_3 "Av. high LB 2nd and 3rd neighbors x Lead"
label var inter_badtetra_nei_1_0_3_corr "Av. high LB 3 neighbors of neighbor x Lead"
label var inter_bad_vc_nei_1_60 "High LB x Lead x Violent crime CC 1960 1st neighbor"

***Controlling for instrument first 3 neighbors
*Set Sample
ivreghdfe perc_cc inter_badtetra_nei_1_3 (vc_pc = inter_badtetra), abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
g SAMPLE_1 = e(sample)==1
*IV
ivreghdfe perc_cc inter_badtetra_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE_1==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
local ff= string(round(e(cdf),.01) , "%9.2f")
est sto control_1
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV CC
ivreghdfe lnpop_cc inter_badtetra_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE_1==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto control_2
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV NCC
ivreghdfe lnpop_ncc inter_badtetra_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE_1==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto control_3
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace
*IV MSA
ivreghdfe lnpop_msa inter_badtetra_nei_1_3 (vc_pc = inter_badtetra) if SAMPLE_1==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto control_4
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local estim "IV" , replace
estadd local ff "`ff'", replace

***Instrumenting VC neighbor
*Set Sample
ivreghdfe perc_cc inter_badtetra_nei_1_0_3_corr inter_badtetra_nei_2_3 (vc_pc vc_pc_nei_1= inter_badtetra inter_badtetra_nei_1), abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
g SAMPLE_2 = e(sample)==1
*IV
ivreghdfe perc_cc inter_badtetra_nei_1_0_3_corr inter_badtetra_nei_2_3 (vc_pc vc_pc_nei_1= inter_badtetra inter_badtetra_nei_1) if SAMPLE_2==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
local ff= string(round(e(cdf),.01) , "%9.2f")
est sto iv_1
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local instrum_nei_nei "YES", replace
estadd local estim "Double IV" , replace
estadd local ff "`ff'", replace
*IV CC
ivreghdfe lnpop_cc inter_badtetra_nei_1_0_3_corr inter_badtetra_nei_2_3 (vc_pc vc_pc_nei_1= inter_badtetra inter_badtetra_nei_1) if SAMPLE_2==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto iv_2
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local instrum_nei_nei "YES", replace
estadd local estim "Double IV" , replace
estadd local ff "`ff'", replace
*IV NCC
ivreghdfe lnpop_ncc inter_badtetra_nei_1_0_3_corr inter_badtetra_nei_2_3 (vc_pc vc_pc_nei_1= inter_badtetra inter_badtetra_nei_1) if SAMPLE_2==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto iv_3
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local instrum_nei_nei "YES", replace
estadd local estim "Double IV" , replace
estadd local ff "`ff'", replace
*IV MSA
ivreghdfe lnpop_msa inter_badtetra_nei_1_0_3_corr inter_badtetra_nei_2_3 (vc_pc vc_pc_nei_1= inter_badtetra inter_badtetra_nei_1) if SAMPLE_2==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto iv_4
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local instrum_nei "YES", replace
estadd local instrum_nei_nei "YES", replace
estadd local estim "Double IV" , replace
estadd local ff "`ff'", replace

***Heterogeneity
*Set sample
reghdfe perc_cc  inter_badtetra inter_bad_vc_nei_1_60, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
g SAMPLE_3 = e(sample)==1
*Set reference levels
sum vc_pc_nei_1_60, det
local p50=`r(p50)'
local p75=`r(p75)'
*Sub
reghdfe perc_cc  inter_badtetra inter_bad_vc_nei_1_60 if SAMPLE_3==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto het_1
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
local coeff_p50= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
local coef_p50 = string(round(`coeff_p50',.001) , "%9.3f")
estadd local addcoef_p50 "`coef_p50'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
matrix define ress = r(V)
local sss_p50 = sqrt(ress[1,1])
local ss_p50 = string(round(`sss_p50',.001) , "%9.3f")
estadd local addss_p50 "`ss_p50'", replace
local coeff_p75= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
local coef_p75 = string(round(`coeff_p75',.001) , "%9.3f")
estadd local addcoef_p75 "`coef_p75'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
matrix define ress = r(V)
local sss_p75 = sqrt(ress[1,1])
local ss_p75 = string(round(`sss_p75',.001) , "%9.3f")
estadd local addss_p75 "`ss_p75'", replace
*Pop CC
reghdfe lnpop_cc  inter_badtetra inter_bad_vc_nei_1_60 if SAMPLE_3==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto het_2
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
local coeff_p50= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
local coef_p50 = string(round(`coeff_p50',.001) , "%9.3f")
estadd local addcoef_p50 "`coef_p50'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
matrix define ress = r(V)
local sss_p50 = sqrt(ress[1,1])
local ss_p50 = string(round(`sss_p50',.001) , "%9.3f")
estadd local addss_p50 "`ss_p50'", replace
local coeff_p75= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
local coef_p75 = string(round(`coeff_p75',.001) , "%9.3f")
estadd local addcoef_p75 "`coef_p75'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
matrix define ress = r(V)
local sss_p75 = sqrt(ress[1,1])
local ss_p75 = string(round(`sss_p75',.001) , "%9.3f")
estadd local addss_p75 "`ss_p75'", replace
*Pop NCC
reghdfe lnpop_ncc  inter_badtetra inter_bad_vc_nei_1_60 if SAMPLE_3==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto het_3
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
local coeff_p50= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
local coef_p50 = string(round(`coeff_p50',.001) , "%9.3f")
estadd local addcoef_p50 "`coef_p50'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
matrix define ress = r(V)
local sss_p50 = sqrt(ress[1,1])
local ss_p50 = string(round(`sss_p50',.001) , "%9.3f")
estadd local addss_p50 "`ss_p50'", replace
local coeff_p75= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
local coef_p75 = string(round(`coeff_p75',.001) , "%9.3f")
estadd local addcoef_p75 "`coef_p75'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
matrix define ress = r(V)
local sss_p75 = sqrt(ress[1,1])
local ss_p75 = string(round(`sss_p75',.001) , "%9.3f")
estadd local addss_p75 "`ss_p75'", replace
*Pop MSA
reghdfe lnpop_msa  inter_badtetra inter_bad_vc_nei_1_60 if SAMPLE_3==1, abs(fipsplace_00 year cdivcode#year) cluster(cdivcodeyear)
est sto het_4
estadd local msafe "YES" , replace
estadd local yearfe "YES" , replace
estadd local cdivyfe "YES", replace
estadd local estim "OLS" , replace
local coeff_p50= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
local coef_p50 = string(round(`coeff_p50',.001) , "%9.3f")
estadd local addcoef_p50 "`coef_p50'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p50'
matrix define ress = r(V)
local sss_p50 = sqrt(ress[1,1])
local ss_p50 = string(round(`sss_p50',.001) , "%9.3f")
estadd local addss_p50 "`ss_p50'", replace
local coeff_p75= _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
local coef_p75 = string(round(`coeff_p75',.001) , "%9.3f")
estadd local addcoef_p75 "`coef_p75'", replace
nlcom _b[inter_badtetra]+_b[inter_bad_vc_nei_1_60]*`p75'
matrix define ress = r(V)
local sss_p75 = sqrt(ress[1,1])
local ss_p75 = string(round(`sss_p75',.001) , "%9.3f")
estadd local addss_p75 "`ss_p75'", replace

***Table
esttab control_1 control_2 control_3 control_4 using table_vcnei_iv.tex, ///
	   prehead("\begin{tabular}{l*{4}{c}} \hline\hline") ///
	   posthead("\hline \\ \multicolumn{5}{c}{Panel A: Controlling for the instrument in the three closest neighbors} \\\\[-1ex]") ///
	   fragment label nocon noobs se ///
	   s(N msafe yearfe cdivyfe instrum_nei estim ff, label("Observations" "MSA FE" "Year FE" "C. div x Year" "Av. high LB 3 neighbors x Lead" "Estimation" "F")) ///
	   drop(inter_badtetra_nei_1_3) star(* 0.10 ** 0.05 *** 0.01) replace
	   
esttab iv_1 iv_2 iv_3 iv_4 using table_vcnei_iv.tex, ///
	   posthead("\hline \\ \multicolumn{5}{c}{Panel B: Instrumenting violent crime of the 1st neighbor} \\\\[-1ex]") ///
	   fragment append nomtitles nonumbers ///
	   label nocon se ///
	   nolines s(N msafe yearfe cdivyfe instrum_nei instrum_nei_nei estim ff, label("Observations" "MSA FE" "Year FE" "C. div x Year" "Av. high LB 2nd and 3rd neighbors x Lead" "Av. high LB 3 neighbors of neighbor x Lead" "Estimation" "F")) ///
	   drop(inter_badtetra_nei_1_0_3_corr inter_badtetra_nei_2_3) star(* 0.10 ** 0.05 *** 0.01) 
	   
esttab het_1 het_2 het_3 het_4 using table_vcnei_iv.tex, ///
	   posthead("\hline \\ \multicolumn{5}{c}{Panel C: Heterogeneity with respect to violent crime of the 1st neighbor in 1960} \\\\[-1ex]") ///
	   fragment append nomtitles nonumbers ///
	   label nocon se ///
	   nolines s(N msafe yearfe cdivyfe estim addcoef_p50 addss_p50 addcoef_p75 addss_p75, label("Observations" "MSA FE" "Year FE" "C. div x Year" "Estimation" "M.E. at p50" "s.e." "M.E. at p75" "s.e." )) ///
	   star(* 0.10 ** 0.05 *** 0.01) ///
	   prefoot("\hline") ///
	   postfoot("\hline\hline \end{tabular}")
